package extractor;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import preprocess.UserFilter;
import trouger.dmt.JoinTableRunner;
import trouger.dmt.JoinType;
import utils.Utils;


public class UserExtractor implements Tool{
	
	public static String getUserProfilePath(String date) {
		return Utils.rootDir + date + "/UserProfile/";
	}
	
	@Override 
	public Configuration getConf() {
		return null;
	}
	
	@Override
	public void setConf(Configuration conf) {
		
	}
	/*
	 * (non-Javadoc)
	 * UserFiltered Table
	 * Path: /group/tbsc-dev/xzwang/clickprediction/$date/UserProfile/
	 * 1.数字id
	 * 2.点击数
	 * 3.32位
	 * 4.Nick
	 * 5.FULLNAME (真实姓名) 
	 * 6.ADDRESS (地址)
	 * 7.CITY (所在城市)
	 * 8.PROV (所在省份)
	 * 9.COUNTRY (国家) 
	 * 10.RATE_SUM (作为卖家的信用分值，只有当等级发生变迁的时候才更新)
	 * 11.RATE_NUM (作为买家的信用分值，只有当等级发生变迁的时候才更新)
	 * 12.BIRTHDATE (生日)
	 * 13.SUSPENDED (用户状态：0 正常;1 未激活;2 删除;3 冻结;-9 CC) 
	 * 14.USER_SESSION_TIME (用户激活时间) 
	 * 15.USER_REGDATE (注册时间)
	 * 16.USER_LEVERL (用户等级) 
	 * 17.USER_STYLE (收费服务类型(按位取与字段，目前定义的二进制位有 1:alisoft网店版用户 2:b2c商城用户[占用先]))
	 * 18.USER_RANK (用户社区等级，论坛改造后没有在使用。现在还是有使用的,在商品举报的地方,有个小二专用, 中用user_rank来判断小二身份...小二身份=46 by yekai)
	 * 19.USER_GENDER (性别)
	 * 20.PROMOTED_TYPE  
	 * */	 
	@Override
	public int run(String[] args) throws Exception {
		String userFilteredFile = UserFilter.getFilteredUserPath(args[0]);
		String output = getUserProfilePath(args[0]);
		String userProfile = "/group/taobao/taobao/hive/s_bmw_users/pt=" + args[0] + "000000/";
		System.out.println("start job ExtractUser...");
		System.out.println("userFilteredFile: " + userFilteredFile);
		System.out.println("userProfile: " + userProfile);
		System.out.println("output: " + output);
		if (Utils.exitSystem(args))
			return -1;
		JoinTableRunner r = new JoinTableRunner();
		r.addSourceTable(userFilteredFile, "UTF-8", "\t");
		r.addSourceTable(userProfile, "UTF-8", "\t");
		r.addKeyGroup("1.1, 2.20");	
		r.setJoinType(JoinType.InnerJoin);
		r.setResultFieldsEx("1.1, 1.2, 2.1, 2.2, 2.4, " +
				"2.5, 2.6, 2.7, 2.8, 2.13, " +
				"2.14, 2.15, 2.16, 2.23, 2.26, " +
				"2.28, 2.30, 2.35, 2.49, 2.50");
		r.setResultTable(output);
		r.run("UserFeatureExtractorJob");
		return 0;		 	
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Utils.checkArgs(args);
		
		// TODO Auto-generated method stub
		int res = ToolRunner.run(new UserExtractor(), args);
		System.exit(res);
	}

}
